package com.example.llm;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.Objects;

public class ChatSession implements Serializable {
    private static final long serialVersionUID = 1L;
    
    private String id;
    private String title;
    private final LocalDateTime createTime;
    private LocalDateTime lastUpdateTime;
    private long lastResponseTime;
    private boolean isPinned;
    private final List<ChatMessage> messages;
    private String systemPrompt;
    private String icon;

    public ChatSession() {
        this.id = UUID.randomUUID().toString();
        this.createTime = LocalDateTime.now();
        this.lastUpdateTime = LocalDateTime.now();
        this.messages = new ArrayList<>();
        this.title = "新会话";
        this.systemPrompt = "You are a helpful assistant.";
        this.isPinned = false;
        this.lastResponseTime = System.currentTimeMillis();
        this.icon = "🤖";
    }

    public void addMessage(String role, String content) {
        messages.add(new ChatMessage(role, content));
        updateLastUpdateTime();
    }

    public String getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        /*if ("新会话".equals(this.title)) {
            System.out.println("不能修改新会话的标题");
            return;
        }*/
        this.title = title;
        updateLastUpdateTime();
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public LocalDateTime getLastUpdateTime() {
        return lastUpdateTime;
    }

    public void updateLastUpdateTime() {
        this.lastUpdateTime = LocalDateTime.now();
    }

    public long getLastResponseTime() {
        return lastResponseTime;
    }

    public void setLastResponseTime(long lastResponseTime) {
        this.lastResponseTime = lastResponseTime;
    }

    public boolean isPinned() {
        return isPinned;
    }

    public void setPinned(boolean pinned) {
        this.isPinned = pinned;
    }

    public List<ChatMessage> getMessages() {
        return messages;
    }

    public String getSystemPrompt() {
        return systemPrompt;
    }

    public void setSystemPrompt(String systemPrompt) {
        this.systemPrompt = systemPrompt;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ChatSession that = (ChatSession) o;
        return id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return title;
    }

    public static class ChatMessage implements Serializable {
        private static final long serialVersionUID = 2L;
        private final String role;
        private final String content;
        private final LocalDateTime timestamp;

        public ChatMessage(String role, String content) {
            this.role = role;
            this.content = content;
            this.timestamp = LocalDateTime.now();
        }

        public String getRole() {
            return role;
        }

        public String getContent() {
            return content;
        }

        public LocalDateTime getTimestamp() {
            return timestamp;
        }
    }
} 